//
//  ChoiceMercChannelAlertVM.swift
//  kxf
//
//  Created by LNS2 on 2024/6/15.
//

import Foundation
import UIKit

class ChoiceMercChannelAlertVM: UIView {
    
    var dataArray = NSArray()
    var labelArray:[UILabel] = [UILabel]()
    var selectIndex = 0
    
    public var tapBlock:((NSDictionary)->())?
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x:0, y: 0, width: SCREEN_WIDHT, height: SCREEN_HEIGHT))
        self.backgroundColor = WHColorWithAlpha(colorStr: "000000", alpha: 0.66)
        self.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(hiddenSelfAction))
        self.addGestureRecognizer(tap)
        
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var whiteView : UIView = {
        let vi = UIView()
        vi.layer.cornerRadius = kFitWidth(12)
        vi.clipsToBounds = true
        vi.backgroundColor = .white
        vi.isUserInteractionEnabled = true
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(nothingToDo))
        vi.addGestureRecognizer(tap)
        
        return vi
    }()
    lazy var titleLabel : UILabel = {
        let lab = UILabel()
        lab.text = "选择商户渠道"
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 18, weight: .medium)
        
        return lab
    }()
    lazy var confirmBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("确认新增", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        btn.layer.cornerRadius = kFitWidth(20)
        btn.clipsToBounds = true
        btn.backgroundColor = .THEME
        
        btn.addTarget(self, action: #selector(confirmBtnAction), for: .touchUpInside)
        
        return btn
    }()
}

extension ChoiceMercChannelAlertVM{
    @objc func labelTapAction(tapSender:UITapGestureRecognizer){
        setNormal()
        let tapLabel = tapSender.view as! UILabel
        selectIndex = tapLabel.tag - 4020
        
        tapLabel.layer.borderColor = UIColor.THEME.cgColor
    }
    @objc func nothingToDo() {
        
    }
    @objc func hiddenSelfAction() {
        self.isHidden = true
    }
    @objc func confirmBtnAction() {
        if self.dataArray.count > 0 {
            if self.tapBlock != nil{
                self.tapBlock!(self.dataArray[self.selectIndex] as! NSDictionary)
            }
            self.isHidden = true
        }else{
            
        }
    }
    func setNormal() {
        for label in labelArray{
            label.layer.borderColor = UIColor.white.cgColor
        }
    }
    func updateUI(array:NSArray) {
        self.dataArray = array
        labelArray.removeAll()
        for i in 0..<self.dataArray.count{
            let dict = self.dataArray[i]as? NSDictionary ?? [:]
            let lab = UILabel()
            lab.text = dict["name"]as? String ?? ""
            lab.textColor = .COLOR_TEXT_BLACK666
            lab.font = .systemFont(ofSize: 16, weight: .regular)
            lab.textAlignment = .center
            lab.layer.cornerRadius = kFitWidth(4)
            lab.clipsToBounds = true
            lab.layer.borderWidth = kFitWidth(1)
            lab.adjustsFontSizeToFitWidth = true
            lab.isUserInteractionEnabled = true
            lab.tag = 4020 + i
            
            if i == 0 {
                lab.layer.borderColor = UIColor.THEME.cgColor
            }else{
                lab.layer.borderColor = UIColor.white.cgColor
            }
            
            labelArray.append(lab)
            whiteView.addSubview(lab)
            
            let tap = UITapGestureRecognizer.init(target: self, action: #selector(labelTapAction(tapSender: )))
            lab.addGestureRecognizer(tap)
            
            let originY = kFitWidth(61) + (kFitWidth(6) + kFitWidth(35)) * CGFloat(i)
            lab.snp.makeConstraints { make in
                make.centerX.lessThanOrEqualToSuperview()
                make.width.equalTo(kFitWidth(107))
                make.height.equalTo(kFitWidth(35))
                make.top.equalTo(originY)
//                make.bottom.equalTo(kFitWidth(-90))
            }
            
        }
        
    }
}
extension ChoiceMercChannelAlertVM{
    func initUI() {
        addSubview(whiteView)
        whiteView.addSubview(titleLabel)
        whiteView.addSubview(confirmBtn)
        
        setConstrait()
    }
    func setConstrait()  {
        whiteView.snp.makeConstraints { make in
            make.center.lessThanOrEqualToSuperview()
            make.width.equalTo(kFitWidth(280))
            make.height.equalTo(kFitWidth(271))
        }
        titleLabel.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.top.equalTo(kFitWidth(22))
        }
        
        confirmBtn.snp.makeConstraints { make in
            make.centerX.lessThanOrEqualToSuperview()
            make.width.equalTo(kFitWidth(114))
            make.height.equalTo(kFitWidth(40))
            make.bottom.equalTo(kFitWidth(-34))
        }
    }
}

